package com.heaven.controller;

import com.heaven.pojo.ChangePassword;
import com.heaven.pojo.Result;
import com.heaven.pojo.User;
import com.heaven.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin(origins="http://localhost:7000")
@RequestMapping("/forget")
public class ForgetController {
    @Autowired
    private UserService userService;
    //1.先验证用户名密码
    //2.修改密码
    @PutMapping
    public Result add_re(@RequestBody ChangePassword changePassword){
        log.info("修改密码信息:{}",changePassword);
        User user = new User();
        user.setUsername(changePassword.getUsername());
        user.setPassword(changePassword.getPassword());
        User e = userService.Login(user);
        if(e != null){
            e.setPassword(changePassword.getNewPassword());
            userService.updatePassword(e);
            return Result.success();
        }
        return Result.error("用户名或密码错误");
    }
}
